package src.ten;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;


public class DictionaryDemo extends JFrame{
    JTextField textField;
    HashMap words;
    JTextArea textArea;
    public DictionaryDemo(){//构造方法
        addWindowListener(new WindowAdapter(){//添加窗口监听事件
            public void windowActivated(WindowEvent e){
                do_this_windowActivated(e);
            }
        });
        setTitle("\u6211\u7684\u7535\u5B50\u8BCD\u5178");//设置标题
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭方法
        setBounds(100,100,300,200);//设置窗体大小以及位置
        JPanel contentPane=new JPanel();//创建容器面板
        contentPane.setBorder(new EmptyBorder(5,5,5,5));//设置边界
        contentPane.setLayout(new BorderLayout(0,0));//设置布局
        setContentPane(contentPane);
        JPanel panel1=new JPanel();//创建面板
        contentPane.add(panel1,BorderLayout.NORTH);//将面板添加到容器并放在北边
        JLabel label=new JLabel("\u8BF7\u8F93\u5165\u8981\u67E5\u8BE2\u7684\u5355\u8BCD\uFF1A");
        panel1.add(label);//创建标签并添加到面板
        textField=new JTextField();//创建文本域
        panel1.add(textField);//将文本域添加到面板
        textField.setColumns(10);//设置10列
        JPanel panel2=new JPanel();//创建面班2
        contentPane.add(panel2,BorderLayout.SOUTH);//将面板2添加到容器并放在南方
        JButton button=new JButton("\u67E5\u8BE2");
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                do_button_actionPerformed(e);
            }
        });
        panel2.add(button);//将按钮添加到面板
        JScrollPane scrollPane=new JScrollPane();//创建带滚动条面板
        contentPane.add(scrollPane,BorderLayout.CENTER);//将滚动条面板添加到容器并方中间
        textArea=new JTextArea();//设置文本域
        textArea.setEditable(false);
        scrollPane.setViewportView(textArea);

    }
    protected void do_this_windowActivated(WindowEvent e){
        words=new HashMap<String,String>();//集合map
        words.put("apple","苹果");
        words.put("banana","香蕉");
        words.put("water","水");
    }
    protected void do_button_actionPerformed(ActionEvent e){

        String text=textField.getText();//获得用户输入的单词
        if(text.isEmpty()){
            JOptionPane.showMessageDialog(this,"请输入要查询的单词",null,JOptionPane.WARNING_MESSAGE);
            return;
        }
        String meaning=(String) words.get(text);//查询单词的含义
        if(meaning==null){
            JOptionPane.showMessageDialog(this,"要查询的单词不存在",null,JOptionPane.WARNING_MESSAGE);
            return;
        }else{
            textArea.setText(meaning);//显示单词含义
        }
    }
    public static void main(String[] args) {
        DictionaryDemo dictionarydemo=new DictionaryDemo();
        dictionarydemo.setVisible(true);


    }


}

